package com.isoftstone.disk.login.service.impl;

import com.isoftstone.disk.login.entity.DiskUserEntity;
import com.isoftstone.disk.login.entity.MenuLevelEnetity;
import com.isoftstone.disk.login.entity.PtUserEntity;
import com.isoftstone.disk.login.repository.LoginDao;
import com.isoftstone.disk.login.service.LoginService;
import com.isoftstone.disk.mybatis.entity.PtMenuEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Service("loginService")
public class LoginServiceImpl implements LoginService {
    private Logger logger = LogManager.getLogger(this.getClass().getName());
    @Resource
    private LoginDao loginDao;

    @Override
    public List<PtUserEntity> queryUserInfoByUserName(String username) {
        return loginDao.queryUserInfoByUserName(username);
    }

    @Override
    public List<String> queryUserAuthByUserId(String userUuid) {
        return loginDao.queryUserAuthByUserId(userUuid);
    }

    @Override
    public List<MenuLevelEnetity> queryMenuLevelByLangId() {

        Locale locale = LocaleContextHolder.getLocale();
        DiskUserEntity diskUserEntity = (DiskUserEntity) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        String userUUID = diskUserEntity.getUserUuid();
        List<PtMenuEntity> list = loginDao.queryMenuLevelByLangId(locale.toString(),userUUID);


        List<MenuLevelEnetity> menuLevelEnetities = new ArrayList<>();
        MenuLevelEnetity menu1 ;
        MenuLevelEnetity menu2 ;
        MenuLevelEnetity menu3 ;
        for(PtMenuEntity ptMenuEntity: list){
            //一级菜单
            if(ptMenuEntity.getMenuLevel() ==  1){
                menu1 = new MenuLevelEnetity();
                menu1.setMenuId(ptMenuEntity.getMenuId());
                menu1.setMenuIcon(ptMenuEntity.getMenuIcon());
                menu1.setMenuName(ptMenuEntity.getMenuName());
                menu1.setMenuUrl(ptMenuEntity.getMenuUrl());
                menuLevelEnetities.add(menu1);
                //二级菜单
                for(PtMenuEntity ptMenuEntity2:list){
                    if(ptMenuEntity.getMenuId().equals(ptMenuEntity2.getParentId())){
                        menu2 = new MenuLevelEnetity();
                        menu2.setMenuId(ptMenuEntity2.getMenuId());
                        menu2.setMenuIcon(ptMenuEntity2.getMenuIcon());
                        menu2.setMenuName(ptMenuEntity2.getMenuName());
                        menu2.setMenuUrl(ptMenuEntity2.getMenuUrl());
                        menu1.addChildList(menu2);
                        //三级菜单
                        for(PtMenuEntity ptMenuEntity3:list){
                            if(ptMenuEntity2.getMenuId().equals(ptMenuEntity3.getParentId())){
                                menu3 = new MenuLevelEnetity();
                                menu3.setMenuId(ptMenuEntity3.getMenuId());
                                menu3.setMenuIcon(ptMenuEntity3.getMenuIcon());
                                menu3.setMenuName(ptMenuEntity3.getMenuName());
                                menu3.setMenuUrl(ptMenuEntity3.getMenuUrl());
                                menu2.addChildList(menu3);
                            }
                        }
                    }
                }

            }
        }


        logger.info(menuLevelEnetities);
        return menuLevelEnetities;
    }
}
